using Microsoft.Extensions.Logging;
using Spring.Expressions;

namespace Spring.Aspects.Exceptions;

/// <summary>
/// Executes an abribtrary Spring Expression Language (SpEL) expression as an action when handling an exception.
/// </summary>
public class ExecuteSpelExceptionHandler : AbstractExceptionHandler
{
    /// <summary>
    /// Initializes a new instance of the <see cref="ExecuteSpelExceptionHandler"/> class.
    /// </summary>
    public ExecuteSpelExceptionHandler()
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="ExecuteSpelExceptionHandler"/> class.
    /// </summary>
    /// <param name="exceptionNames">The exception names.</param>
    public ExecuteSpelExceptionHandler(string[] exceptionNames)
        : base(exceptionNames)
    {
    }

    /// <summary>
    /// Handles the exception.
    /// </summary>
    /// <returns>The return value from handling the exception, if not rethrown or a new exception is thrown.</returns>
    public override object HandleException(IDictionary<string, object> callContextDictionary)
    {
        try
        {
            IExpression expression = Expression.Parse(ActionExpressionText);
            expression.GetValue(null, callContextDictionary);
        }
        catch (Exception e)
        {
            string message = "Was not able to evaluate action expression [" + ActionExpressionText + "]";
            log.LogWarning(e, message);
        }

        return null;
    }
}
